﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Code4WP7SDK.Controls
{
    public class OddEvenListBox : ListBox
    {
        #region Dependency Properties
        public DataTemplateSelector ItemTemplateSelector
        {
            get { return (DataTemplateSelector)GetValue(ItemTemplateSelectorProperty); }
            set { SetValue(ItemTemplateSelectorProperty, value); }
        }

        public static readonly DependencyProperty ItemTemplateSelectorProperty =
            DependencyProperty.Register(
                                        "ItemTemplateSelector",
                                        typeof(DataTemplateSelector),
                                        typeof(OddEvenListBox),
                                        new PropertyMetadata(OnTemplateChanged)
                                       );

        public static void OnTemplateChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
        {

        }
        #endregion
        
        protected override void PrepareContainerForItemOverride(DependencyObject element, object item)
        {
            base.PrepareContainerForItemOverride(element, item);
            int index = ItemContainerGenerator.IndexFromContainer(element);
            ListBoxItem listBoxitem = element as ListBoxItem;
            //横向扩展
            listBoxitem.HorizontalContentAlignment = HorizontalAlignment.Stretch;
            //if (index % 2 == 0)
            //{
            //    listBoxitem.HorizontalContentAlignment = HorizontalAlignment.Right;
            //}
            //else
            //{
            //    listBoxitem.HorizontalContentAlignment = HorizontalAlignment.Left;
            //}
            if (listBoxitem != null)
            {
                listBoxitem.ContentTemplate = this.ItemTemplateSelector.SelectTemplateByIndex(index);
            }
        }

        protected override void OnItemsChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            base.OnItemsChanged(e);
        }
    }
    public class DataTemplateSelector
    {
        public virtual DataTemplate SelectTemplateByIndex(int index)
        {
            return null;
        }
        public virtual DataTemplate SelectTemplateByItemType(object item, DependencyObject container)
        {
            return null;
        }
    }

    public class OddEvenTemplateSelector : DataTemplateSelector
    {
        public DataTemplate RightTemplate { get; set; }

        public DataTemplate LeftTemplate { get; set; }

        public override DataTemplate SelectTemplateByIndex(int index)
        {
            if (index % 2 == 0)
            {
                return this.RightTemplate;
            }
            else
            {
                return this.LeftTemplate;
            }
        }
    }
}
